1
|
|
|
/** |
2
|
|
|
|
3
|
|
|
Copyright (c) 2018 Intuit |
4
|
|
|
# |
5
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
# you may not use this file except in compliance with the License. |
7
|
|
|
# You may obtain a copy of the License at |
8
|
|
|
# |
9
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
# |
11
|
|
|
# Unless required by applicable law or agreed to in writing, software |
12
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
# See the License for the specific language governing permissions and |
15
|
|
|
# limitations under the License. |
16
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @namespace AuthResponse |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
'use strict'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AuthResponse |
27
|
|
|
* @property {Token} token |
28
|
|
|
* @property {Response} response |
29
|
|
|
* @property {string} body |
30
|
|
|
* @property {object} json |
31
|
|
|
* @property {string} intuit_tid |
32
|
|
|
*/ |
33
|
|
|
function AuthResponse(params) { |
34
|
|
|
this.token = params.token || ''; |
35
|
|
|
this.response = params.response || ''; |
36
|
|
|
this.body = params.responseText || ''; |
37
|
|
|
this.json = null; |
38
|
|
|
this.intuit_tid = params.intuit_tid || ''; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Process Response |
43
|
|
|
* @param response |
44
|
|
|
*/ |
45
|
|
|
AuthResponse.prototype.processResponse = function processResponse(response) { |
46
|
|
|
this.response = response || ''; |
47
|
|
|
|
48
|
|
|
// Handle response data |
49
|
|
|
if (response) { |
50
|
|
|
// Set intuit_tid from headers if available |
51
|
|
|
this.intuit_tid = (response.headers && response.headers.intuit_tid) || ''; |
52
|
|
|
|
53
|
|
|
if (response.data) { |
54
|
|
|
// Handle axios response |
55
|
|
|
this.body = typeof response.data === 'string' ? response.data : JSON.stringify(response.data); |
56
|
|
|
|
57
|
|
|
// Handle QuickBooks API error response |
58
|
|
|
if (response.data.Fault) { |
59
|
|
|
this.json = { |
60
|
|
|
Fault: response.data.Fault, |
61
|
|
|
time: response.data.time || new Date().toISOString(), |
62
|
|
|
intuit_tid: this.intuit_tid, |
63
|
|
|
}; |
64
|
|
|
// Store the full response including headers and status |
65
|
|
|
this.response = { |
66
|
|
|
...response, |
67
|
|
|
data: this.json, |
68
|
|
|
status: response.status || 400, |
69
|
|
|
statusText: response.statusText || 'Bad Request', |
70
|
|
|
}; |
71
|
|
|
} else { |
72
|
|
|
// Store the raw response data |
73
|
|
|
this.json = response.data; |
74
|
|
|
// Store the full response for successful responses |
75
|
|
|
this.response = { |
76
|
|
|
...response, |
77
|
|
|
data: this.json, |
78
|
|
|
}; |
79
|
|
|
} |
80
|
|
|
} else if (response.body) { |
81
|
|
|
// Handle other response types |
82
|
|
|
this.body = response.body; |
83
|
|
|
try { |
84
|
|
|
const parsedBody = typeof response.body === 'string' ? JSON.parse(response.body) : response.body; |
85
|
|
|
|
86
|
|
|
// Handle QuickBooks API error response |
87
|
|
|
if (parsedBody.Fault) { |
88
|
|
|
this.json = { |
89
|
|
|
Fault: parsedBody.Fault, |
90
|
|
|
time: parsedBody.time || new Date().toISOString(), |
91
|
|
|
intuit_tid: this.intuit_tid, |
92
|
|
|
}; |
93
|
|
|
// Store the full response including headers and status |
94
|
|
|
this.response = { |
95
|
|
|
...response, |
96
|
|
|
data: this.json, |
97
|
|
|
status: response.status || 400, |
98
|
|
|
statusText: response.statusText || 'Bad Request', |
99
|
|
|
}; |
100
|
|
|
} else { |
101
|
|
|
// Store the raw response data |
102
|
|
|
this.json = parsedBody; |
103
|
|
|
// Store the full response for successful responses |
104
|
|
|
this.response = { |
105
|
|
|
...response, |
106
|
|
|
data: this.json, |
107
|
|
|
}; |
108
|
|
|
} |
109
|
|
|
} catch (e) { |
110
|
|
|
this.json = null; |
111
|
|
|
this.response = response; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
this.body = ''; |
115
|
|
|
this.json = null; |
116
|
|
|
this.response = response; |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
|
|
this.body = ''; |
120
|
|
|
this.json = null; |
121
|
|
|
this.response = null; |
122
|
|
|
} |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get Token |
127
|
|
|
* * |
128
|
|
|
* @returns {object} token |
129
|
|
|
*/ |
130
|
|
|
AuthResponse.prototype.getToken = function getToken() { |
131
|
|
|
return this.token.getToken(); |
132
|
|
|
}; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get Token |
136
|
|
|
* * |
137
|
|
|
* @returns {string} text |
138
|
|
|
*/ |
139
|
|
|
AuthResponse.prototype.text = function text() { |
140
|
|
|
return this.body; |
141
|
|
|
}; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get Token |
145
|
|
|
* * |
146
|
|
|
* @returns {Number} statusCode |
147
|
|
|
*/ |
148
|
|
|
AuthResponse.prototype.status = function status() { |
149
|
|
|
return this.response.status; |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get response headers |
154
|
|
|
* * |
155
|
|
|
* @returns {Object} headers |
156
|
|
|
*/ |
157
|
|
|
AuthResponse.prototype.headers = function headers() { |
158
|
|
|
return this.response.headers; |
159
|
|
|
}; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Is Response valid { response is valid ? } |
163
|
|
|
* * |
164
|
|
|
* @returns {*|boolean} |
165
|
|
|
*/ |
166
|
|
|
AuthResponse.prototype.valid = function valid() { |
167
|
|
|
return this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300; |
168
|
|
|
}; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get Json () { returns response as JSON } |
172
|
|
|
* * |
173
|
|
|
* @return {object} json |
174
|
|
|
* @throws {Error} If response cannot be parsed as JSON |
175
|
|
|
*/ |
176
|
|
|
AuthResponse.prototype.getJson = function getJson() { |
177
|
|
|
// If we already have parsed JSON, return it |
178
|
|
|
if (this.json !== null) { |
179
|
|
|
return this.json; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// Try to parse the body if we have one |
183
|
|
|
if (this.body) { |
184
|
|
|
try { |
185
|
|
|
this.json = typeof this.body === 'string' ? JSON.parse(this.body) : this.body; |
186
|
|
|
|
187
|
|
|
// Handle QuickBooks API error response |
188
|
|
|
if (this.json.Fault) { |
189
|
|
|
this.json = { |
190
|
|
|
Fault: this.json.Fault, |
191
|
|
|
time: this.json.time || new Date().toISOString(), |
192
|
|
|
intuit_tid: this.intuit_tid, |
193
|
|
|
}; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return this.json; |
197
|
|
|
} catch (e) { |
198
|
|
|
throw new Error(`Failed to parse response as JSON: ${e.message}`); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// If we have no body, return null |
203
|
|
|
return null; |
204
|
|
|
}; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get Intuit tid |
208
|
|
|
* * |
209
|
|
|
* @returns {string} intuit_tid |
210
|
|
|
*/ |
211
|
|
|
AuthResponse.prototype.getIntuitTid = function getIntuitTid() { |
212
|
|
|
return this.intuit_tid; |
213
|
|
|
}; |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* isContentType |
217
|
|
|
* * |
218
|
|
|
* @returns {boolean} isContentType |
219
|
|
|
*/ |
220
|
|
|
AuthResponse.prototype.isContentType = function isContentType(contentType) { |
221
|
|
|
return this.getContentType().indexOf(contentType) > -1; |
222
|
|
|
}; |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* getContentType |
226
|
|
|
* * |
227
|
|
|
* @returns {string} getContentType |
228
|
|
|
*/ |
229
|
|
|
AuthResponse.prototype.getContentType = function getContentType() { |
230
|
|
|
return this.response.headers[AuthResponse._contentType] || ''; |
231
|
|
|
}; |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* isJson |
235
|
|
|
* * |
236
|
|
|
* @returns {boolean} isJson |
237
|
|
|
*/ |
238
|
|
|
AuthResponse.prototype.isJson = function isJson() { |
239
|
|
|
return this.isContentType('application/json'); |
240
|
|
|
}; |
241
|
|
|
|
242
|
|
|
AuthResponse._contentType = 'content-type'; |
243
|
|
|
AuthResponse._jsonContentType = 'application/json'; |
244
|
|
|
AuthResponse._urlencodedContentType = 'application/x-www-form-urlencoded'; |
245
|
|
|
|
246
|
|
|
module.exports = AuthResponse; |
247
|
|
|
|